<?php

namespace App\Services;

use Illuminate\Validation\ValidationException;
use Carbon\Carbon;


// service 层，查询 ORM 的唯一途径
abstract class Service
{
    // 直接抛出错误
    static protected function throwValidationException(string $key, array $msgArray): void
    {
        throw ValidationException::withMessages([
            $key => $msgArray,
        ]);
    }


    // 递归生成树形结构
    protected static function buildTree(array $data, ?int $parentId = null): array
    {
        // 如果parentId为null，则查找所有顶级节点（在结果集中但父节点不在结果集中的节点）
        if ($parentId === null) {
            $tree = [];
            // 获取所有部门ID
            $allIds = array_column($data, 'id');

            foreach ($data as $item) {
                // 如果父节点ID不在结果集中，或者父节点ID为0，那么这个节点就是顶级节点
                if (!in_array($item['parentId'], $allIds) || $item['parentId'] == 0) {
                    $children = self::buildTree($data, $item['id']);
                    if (!empty($children)) {
                        $item['children'] = $children;
                    }
                    $tree[] = $item;
                }
            }
            return $tree;
        }

        // 常规的树形结构构建
        $tree = [];
        foreach ($data as $item) {
            if ($item['parentId'] == $parentId) {
                $children = self::buildTree($data, $item['id']);
                if (!empty($children)) {
                    $item['children'] = $children;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }

    // 封装树形结构
    protected static function buildOptionsTree(array $data, int $parentId = 0): array
    {
        $tree = [];
        foreach ($data as $item) {
            if ($item['parentId'] == $parentId) {
                $children = self::buildOptionsTree($data, $item['value']);
                unset($item['parentId']);
                if (!empty($children)) {
                    $item['children'] = $children;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }

    // 递归生成树形结构（不返回 parentId）
    protected static function buildTreeNotParentId(array $data, ?int $parentId = null): array
    {
        $tree = [];
        $allIds = array_column($data, 'id');

        foreach ($data as $item) {
            $isTop = $parentId === null
                ? (!in_array($item['parentId'], $allIds) || $item['parentId'] == 0)
                : $item['parentId'] == $parentId;

            if ($isTop) {
                $children = self::buildTreeNotParentId($data, $item['id']);
                if (!empty($children)) {
                    $item['children'] = $children;
                }
                unset($item['parentId']); // 删除 parentId
                unset($item['id']); // 删除 id
                $tree[] = $item;
            }
        }

        return $tree;
    }

    protected static function parseDateRangeToUtc(string $time): Carbon {
        return Carbon::parse($time, 'Asia/Shanghai')->timezone('UTC');
    }
}
